package com.sunny.fetch.dao.common;

import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.fastjson.JSONObject;
import com.sunny.fetch.dao.enumtype.DBType;
import com.sunny.fetch.dao.exception.DataSourceInitException;
import org.apache.commons.lang3.StringUtils;

import javax.sql.DataSource;

/**
 * @Author: sublun
 * @Date: 2021/4/2 17:43
 */
public class DBConfig {
    private DBType dialect;
    private String driverClassName;
    private String url;
    private String username;
    private String password;
    private volatile DataSource dataSource;

    public DBConfig() {

    }

    public DBConfig(DataSource dataSource) {
        this.dataSource = dataSource;
    }

    public DBConfig(String driverClassName, String url, String username, String password) {
        this.driverClassName = driverClassName;
        this.url = url;
        this.username = username;
        this.password = password;
    }

    public DBConfig(JSONObject config) {
        this.driverClassName = config.getString("driverClassName");
        this.url = config.getString("url");
        this.username = config.getString("username");
        this.password = config.getString("password");
    }

    public DataSource getDataSource() throws DataSourceInitException {
        if (dataSource == null) {
            synchronized (this) {
                if (dataSource == null) {
                    dataSource = createDataSource();
                }
            }
        }
        return dataSource;
    }

    private DataSource createDataSource() throws DataSourceInitException {
        checkParam();
        DruidDataSource dataSource = null;
        try {
            dataSource = new DruidDataSource();
            dataSource.setDriverClassName(driverClassName);
            dataSource.setUrl(url);
            dataSource.setUsername(username);
            dataSource.setPassword(password);
        } catch (Exception e) {
            throw new DataSourceInitException("DataSource创建失败", e);
        }
        return dataSource;
    }

    private Boolean checkParam() throws DataSourceInitException {
        if (StringUtils.isAnyEmpty(driverClassName, url, username, password)) {
            throw new DataSourceInitException("数据库连接参数缺失");
        }
        return true;
    }

    public void setDataSource(DataSource dataSource) {
        this.dataSource = dataSource;
    }

    public DBType getDialect() {
        return dialect;
    }

    public void setDialect(DBType dialect) {
        this.dialect = dialect;
    }

    public String getDriverClassName() {
        return driverClassName;
    }

    public void setDriverClassName(String driverClassName) {
        this.driverClassName = driverClassName;
    }

    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }
}
